// Caculator.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "BieuThuc.h"
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <string>
#include <vector>
using namespace std;

BieuThuc* fillTree(vector<double>& inputs, int& position)
{
	int k = (int)inputs[position++];
	BieuThuc* x;
	switch(k)
	{

	case 0:
		x = new BTHang(inputs[position++]);
		return x;
		break;

	case 1:
		x = new BTDon(inputs[position++],(int)inputs[position++]);
		return x;
		break;

	case 2:
	case 3:
	case 4:
		if(k == 2)
			x = new BTTong();
		else if(k == 3 )
			x = new BTTich();
		else if (k == 4)
			x = new BTChia();

		for(int i=0;i<2;i++)
		{
			BieuThuc* u = fillTree(inputs, position);
			x->themBieuThuc(u);
			BieuThuc* v = fillTree(inputs, position);
			x->themBieuThuc(v);

			return x;
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	BTPhuc bt;
	int position =0;
	string STRING;
	vector<double> inputs;
	ifstream infile;
	infile.open ("input.txt");
	if (infile.is_open())
		while(!infile.eof())
        {
	        getline(infile,STRING);
			inputs.push_back(atof (STRING.c_str()));
        }
	infile.close();
	bt.themBieuThuc(fillTree(inputs,position));
	system ("pause");
}


